package com.ehome.poct.core.dao.mybatis;

import com.ehome.poct.base.api.dto.device.TestDeviceDTO;
import com.ehome.poct.core.api.dto.check.GoldCheckAlgoRspDTO;
import com.ehome.poct.core.dao.entity.check.TestDataAlgoDetailDO;
import com.ehome.poct.core.dao.entity.check.TestDataDetailDO;
import com.ehome.poct.core.dao.entity.check.TestDataErrorDetailDO;
import com.ehome.poct.core.dao.entity.check.TestPoctReqHistoryDO;
import com.ehome.poct.core.dao.mybatis.mapper.TestDataAlgoDetailMapper;
import com.ehome.poct.core.dao.mybatis.mapper.TestDataDetailMapper;
import com.ehome.poct.core.dao.mybatis.mapper.TestDataErrorDetailMapper;
import com.ehome.poct.core.repository.check.GoldRequestLogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 金标仪算法请求日志
 *
 * @author gyl
 * @since 2022/3/17 14:28
 **/
@Repository
public class GoldRequestLogRepositoryImpl implements GoldRequestLogRepository {

    @Autowired
    private TestDataErrorDetailMapper testDataErrorDetailMapper;

    @Autowired
    private TestDataAlgoDetailMapper dataAlgoDetailMapper;

    @Autowired
    private TestDataDetailMapper testDataDetailMapper;

    @Override
    public Long saveErrorDetailLog(TestDeviceDTO device, String path, String bucketName, GoldCheckAlgoRspDTO rspDTO) {
        var testDataErrorDetailDO = toErrorDetailLog(device, path, bucketName, rspDTO);
        testDataErrorDetailMapper.insert(testDataErrorDetailDO);
        return testDataErrorDetailDO.getTestDataErrorDetailId();
    }

    @Override
    public void saveTestDataAlgoDetail(String originData, Long testDataErrorDetailId) {
        TestDataAlgoDetailDO testDataAlgoDetailDO = new TestDataAlgoDetailDO(originData, testDataErrorDetailId);
        dataAlgoDetailMapper.insert(testDataAlgoDetailDO);
    }

    @Override
    public Long saveDetailLog(TestDeviceDTO device, String path, String bucketName, GoldCheckAlgoRspDTO rspDTO) {
        var testDataDetailDO = toDetailLog(device, path, bucketName, rspDTO);
        testDataDetailMapper.insert(testDataDetailDO);
        return testDataDetailDO.getTestDataDetailId();
    }

    @Override
    public void saveAlgoReqLog(String deviceId, Long clinicId, String reqUrl, String reqJson, Long testDataDetailId) {
        TestPoctReqHistoryDO testPoctReqHistoryDO = new TestPoctReqHistoryDO();
        testPoctReqHistoryDO.setDeviceSno(deviceId);
        testPoctReqHistoryDO.setTermClinicId(clinicId);
        testPoctReqHistoryDO.setReqUrl(reqUrl);
        testPoctReqHistoryDO.setReqJson(reqJson);
        testPoctReqHistoryDO.setTestDataDetailId(testDataDetailId);

    }


    private TestDataErrorDetailDO toErrorDetailLog(TestDeviceDTO device, String path, String bucketName, GoldCheckAlgoRspDTO rspDTO) {
        if (device == null && null == rspDTO) {
            return null;
        }
        TestDataErrorDetailDO testDataErrorDetailDO = new TestDataErrorDetailDO();
        if (null != device) {
            testDataErrorDetailDO.setSno(device.getSno());
            testDataErrorDetailDO.setTermClinicId(device.getTermClinicId());
        }
        testDataErrorDetailDO.setPicId(path);
        testDataErrorDetailDO.setBucketName(bucketName);
        if (null != rspDTO) {
            testDataErrorDetailDO.setStripTypeCode(null == rspDTO.getData() ? null : rspDTO.getData().getTypeId());
            testDataErrorDetailDO.setStripBatch(null == rspDTO.getData() ? null : rspDTO.getData().getBatchNo());
            testDataErrorDetailDO.setErrorCode(rspDTO.getErrorCode());
            testDataErrorDetailDO.setResult(rspDTO.getMsg());
            testDataErrorDetailDO.setAlgoPicId(null == rspDTO.getData() ? null : rspDTO.getData().getImagePath());
        }
        testDataErrorDetailDO.setCreateTime(LocalDateTime.now());
        return testDataErrorDetailDO;
    }

    public TestDataDetailDO toDetailLog(TestDeviceDTO device, String path, String bucketName, GoldCheckAlgoRspDTO rspDTO) {
        if (device == null && null == rspDTO) {
            return null;
        }
        TestDataDetailDO testDataDetailDO = new TestDataDetailDO();
        if (null != device) {
            testDataDetailDO.setSno(device.getSno());
            testDataDetailDO.setTermClinicId(device.getTermClinicId());
        }
        testDataDetailDO.setPicId(path);
        testDataDetailDO.setBucketName(bucketName);
        if (null != rspDTO) {
            testDataDetailDO.setStripTypeCode(null == rspDTO.getData() ? null : rspDTO.getData().getTypeId());
            testDataDetailDO.setStripBatch(null == rspDTO.getData() ? null : rspDTO.getData().getBatchNo());
            testDataDetailDO.setErrorCode(rspDTO.getErrorCode());
            testDataDetailDO.setResult(rspDTO.getMsg());
            testDataDetailDO.setAlgoPicId(null == rspDTO.getData() ? null : rspDTO.getData().getImagePath());
        }
        testDataDetailDO.setValue1(getDesityValues(rspDTO.getData().getDensityValues(), 1));
        testDataDetailDO.setValue2(getDesityValues(rspDTO.getData().getDensityValues(), 2));
        testDataDetailDO.setValue3(getDesityValues(rspDTO.getData().getDensityValues(), 3));
        testDataDetailDO.setValue4(getDesityValues(rspDTO.getData().getDensityValues(), 4));

        return testDataDetailDO;
    }

    private BigDecimal getDesityValues(BigDecimal[] values, int position) {
        if (null != values && values.length >= position) {
            return values[position - 1];
        }
        return null;
    }
}
